package book.read;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import book.dao.impl.BookDaoImpl;
import book.pojo.Book;

public class BookReadFile {

	private static BookDaoImpl bdi;
	public static void main(String[] args) throws JMSException  {
		// TODO Auto-generated method stub

		ConnectionFactory factory=new ActiveMQConnectionFactory("tcp://10.222.29.152:61616");
		Destination queueDestination=new ActiveMQQueue("books");
		Connection connection =factory.createConnection();
		connection.start();
		Session session=connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		final MessageConsumer mc=session.createConsumer(queueDestination);
	
		final ObjectMapper oMapper=new ObjectMapper();
		bdi=new BookDaoImpl();
		long start=System.currentTimeMillis();
		for(int i=0;i<11999;i++)
		{
			TextMessage msg=(TextMessage) mc.receive();
			String temp=msg.getText();
			Book tempBook=new Book();
			try {
				tempBook=oMapper.readValue(temp, Book.class);
				bdi.addBook(tempBook);
				System.out.println("add one book");
			} catch (JsonParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			msg.acknowledge();
		}
		mc.close();
		session.close();
		connection.close();
//		mc.setMessageListener(new MessageListener() {
//			
//			public void onMessage(Message arg0) {
//				// TODO Auto-generated method stub
//				TextMessage msg = null;
//				msg = (TextMessage)arg0;
//					String bookOfJsonString;
//					try {
//						bookOfJsonString = msg.getText();
//						Book temp=oMapper.readValue(bookOfJsonString, Book.class);
//						bdi.addBook(temp);
//					} catch (JMSException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					} catch (JsonParseException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					} catch (JsonMappingException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					} catch (IOException e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//			}
//		});
		long end=System.currentTimeMillis();
		System.out.println("load "+" books need time "+(end-start));
	}

}
